
import * as math from 'mathjs';

// 以(x,y)为参考点缩放，缩放比例：scale
export function _scale(originalMatrix, scale, x, y) {
    return math.multiply(
        _translationMatrix(x, y),
        _scaleMatrix(scale),
        _translationMatrix(-x, -y),
        originalMatrix)

}
// 移动(x,y)
export function _translation(originalMatrix, x, y) {
    return math.multiply(_translationMatrix(x, y), originalMatrix)
}
// 以(x,y)为参考点旋转，旋转角度：angle
export function _rotate(originalMatrix, angle, x, y) {
    return math.multiply(
        _translationMatrix(x, y),
        _rotateMatrix(angle),
        _translationMatrix(-x, -y),
        originalMatrix)
}
// 得到移动的变换矩阵
export function _translationMatrix(x, y) {
    return math.matrix([
        [1, 0, x],
        [0, 1, y],
        [0, 0, 1],
    ])
}
// 得到以(0,0)为参考点进行缩放的变换矩阵
export function _scaleMatrix(scale) {
    return math.matrix([
        [scale, 0, 0],
        [0, scale, 0],
        [0, 0, 1],
    ])
}
// 得到以(0,0)为参考点进行旋转的变换矩阵
export function _rotateMatrix(angle) {
    // 将角度转换为弧度
    const radian = math.unit(angle, 'deg').to('rad').value;
    return math.matrix([
        [Math.cos(radian), -Math.sin(radian), 0],
        [Math.sin(radian), Math.cos(radian), 0],
        [0, 0, 1]
    ])
}
// 得到以(cx,cy)为参考点，从(x1,y1)旋转到(x2,y2)的旋转角度，单位角度
export function _rotateAngle(cx, cy, x1, y1, x2, y2) {
    // 计算向量
    const vector1 = { x: x1 - cx, y: y1 - cy };
    const vector2 = { x: x2 - cx, y: y2 - cy };

    // 计算向量的叉积和点积
    const crossProduct = vector1.x * vector2.y - vector1.y * vector2.x;
    const dotProduct = vector1.x * vector2.x + vector1.y * vector2.y;

    // 计算夹角
    return Math.atan2(crossProduct, dotProduct) * (180 / Math.PI);
}
